﻿using HPIT.RentHouse.Admin.Filters;
using HPIT.RentHouse.Admin.Models;
using HPIT.RentHouse.Common;
using RentHouse.DTO;
using RentHouse.IService;
using RentHouse.Service;
using RentHouse.Service.Entityes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace RentHouse.Admin.Controllers
{
    [Authorize]
    public class PermissionsController : Controller
    {
        // GET: Permission

        private IPermissionService _permissionService;
        public PermissionsController(IPermissionService permissionService)
        {
            _permissionService = permissionService;
        }

        [CheckMyPermission("lookAdmin")]
        public ActionResult Index()
        {
            var list = _permissionService.GetList();
            return View();
        }
        public ActionResult Index1()
        {
            return View();
        }
        
        public ActionResult Add()
        {
            return View();
        }

        //添加
        [HttpPost]
        public ActionResult AddPer(PermissionDTO dto)
        {
            AjaxResult result = _permissionService.Add(dto);
            return Json(result);
        }
        
        //修改
        public ActionResult Edit(long Id)
        {
            var lists = _permissionService.Get(Id);
            return View(lists);
        }
        [HttpPost]
        public ActionResult EditPer(PermissionDTO dto)
        {
            AjaxResult result = _permissionService.Edit(dto);
            return Json(result);
        }

        [CheckMyPermission("deletPermission")]

        //单个删除
        [HttpPost]
        public ActionResult Delete(long Id)
        {
            AjaxResult result = _permissionService.Delete(Id);
            return Json(result);
        }

        //批量删除
        [HttpPost]
        public ActionResult DeleteBatch(List<long>ids)
        {
            AjaxResult result = _permissionService.DeleteBatch(ids);
            return Json(result);
        }

        public ActionResult Detail(long Id)
        {
            IPermissionService permissionService = new PermissionService();
            PermissionDTO permissionDTO = permissionService.Get(Id);
            return View(permissionDTO);
        }

        public ActionResult GetList(int start,int length,string name)
        {
            //1.接口层写分页方法
            //2.service实现接口方法
            //3.控制器调用
            int count = 0;
            var list = _permissionService.GetPagedList(start, length,name, ref count);
            PageModel pageModel = new PageModel();
            pageModel.data = list;
            pageModel.recordsFiltered = count;
            pageModel.recordsTotal = count;
            return Json(pageModel);
        }
    } 
}